import {action, observable} from 'mobx';
import * as accessViewApi from '../../apis/performance/accessViewApi';
import * as accessAuditListApi from '../../apis/performance/accessAuditListApi';
import {WeaLocaleProvider} from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-评分页面store
 */
export default class AccessViewStore {
    @observable tabs = [];
    @observable loading = false; // 页面初始化的loading状态：数据加载成功前后前使用
    @observable yeardata = []; //年集合
    @observable nowyear = "";//当前年
    @observable nowmonth = "";//当前月
    @observable nowseason = "";  //当前季度
    @observable titlename = "";//标题
    @observable type1 = "";//考核类型1-月度 2-季度 3-半年 4-年度
    @observable type2 = "";//考核具体月份或者季度
    @observable msg = "";//返回错误信息
    cstatus = ""; //当前状态
    ccolor = "";//状态颜色
    pesronstr = "";//当前审批或者考核人信息字符串
    canback = false; //能否退回
    canreset = false; //能否重新考核
    canrecover = false;//能否收回考核
    canbackpre = false;//能否退回上一级
    canauditbackpre = false;//审批人能否退回上一级
    checksize = 0;
    @observable canedit = false; //是否评分
    caninit = false; //是否初始化
    @observable canupload = false;//是否可以附件上传
    canview = false; //能否可看
    canlog = false;//是否能查看日志
    canaudit = false; //能否审批
    scoreid = "";//当前评分id
    @observable fbInfoList = [];//反馈信息列表
    @observable fbcontent = "";//反馈内容
    @observable logList = [];//日志列表
    @observable agentmessage = "";//代理人信息
    currentpage = 1;//日志列表当前页
    total = 1;//日志列表总数
    isplan = false;//是否启用报告
    resourcename = "";//当前被考核人名称
    resourceid = "";//当前被考核人id
    tableHeader = [];//评分人列表
    titlestr = "";
    itemcount = 0;//考核指标数量
    @observable datalist = [];//数据集合
    scoremin = -5;//评分最小值
    scoremax = 5;//评分最大值
    revisemin = -2;//修正分最小值
    revisemax = 2;//修正分最大值
    remark = "";
    category = "";
    maxUploadSize = "";
    uploading = false;//是否有附件上传
    fileids = "";//新增的附件id
    delfileids = "";//删除的ids
    uploadref = "";//附件上传ref
    currenttype = "";//当前操作Operation
    result_score = "";//最终得分
    viewdate = "";//最后查看时间
    tablewidth = "";
    leftwidth = "";
    linkUrlList = [];//相关链接数组
    rowcount = 6;
    hascate = false;
    edition = 0;//当前版本

    // 评分页面请求
    @action
    accessView = (params) => {
        this.loading = true;
        accessViewApi.accessView(params).then(action(data => {
            this.type1 = data.type1;
            this.type2 = data.type2;
            this.nowyear = data.year;
            if (this.type1 == 1) {
                this.nowmonth = data.type2;
            }
            if (this.type1 == 2) {
                this.nowseason = data.type2;
            }
            this.resourceid = data.resourceid;
            this.resourcename = data.resourcename;
            this.titlename = data.titlename;
            this.tabs = data.stabList;
            this.scoreid = data.scoreid;
            this.fileids = "";
            this.delfileids = "";
            this.uploading = false;
            this.total = 1;
            this.currentpage = 1;
            if (data.status == 1) {
                this.ccolor = data.ccolor;
                this.pesronstr = data.pesronstr;
                this.cstatus = data.cstatus;
                this.scoremin = data.scoremin;
                this.scoremax = data.scoremax;
                this.revisemin = data.revisemin;
                this.revisemax = data.revisemax;
                this.remark = data.remark;
                this.viewdate = data.viewdate;
                this.result_score = data.result_score;
                this.category = data.docsecid;
                this.maxUploadSize = data.maxUploadSize;
                this.itemcount = data.itemcount;
                this.titlestr = data.titlestr;
                this.tableHeader = data.tableHeader;
                this.tablewidth = data.tablewidth;
                this.leftwidth = data.leftwidth;
                this.agentmessage = data.agentmessage;
                this.rowcount = data.rowcount || 6;
                this.linkUrlList = data.linkUrlList;
                this.isplan = data.isplan;
                this.canlog = data.canlog;
                this.canback = data.canback;
                this.canrecover = data.canrecover;
                this.canbackpre = data.canbackpre;
                this.canauditbackpre = data.canauditbackpre;
                this.checksize = data.checksize;
                this.canreset = data.canreset;
                this.canedit = data.canedit;
                this.hascate = data.hascate;
                this.datalist = data.dataList;
                if (data.canedit && data.countmapList && data.countmapList.length > 0) {
                    data.countmapList.map((element, index) => {
                        this.updateNormScore(element.index, 1);
                    })
                }
                this.edition = data.edition;
                this.canupload = data.canedit;
                this.caninit = data.caninit;
                this.canview = data.canview;
                this.canaudit = data.canaudit;
                this.logList = data.logList;
                this.msg = "1";
                if (this.caninit && params.doback) {
                    params.doback();
                }
            } else {
                this.clearValue();
                this.msg = data.msg;
            }
            this.loading = false;
        }));
    };

    @action
    clearValue() {
        this.cstatus = "";
        this.ccolor = "";
        this.agentmessage = "";
        this.pesronstr = "";
        this.canback = false;
        this.canreset = false;
        this.canedit = false;
        this.caninit = false;
        this.canaudit = false;
        this.canrecover = false;//能否收回考核
        this.canbackpre = false;//能否退回上一级
        this.canauditbackpre = false;
        this.checksize = 0;
        this.canlog = false;
        this.isplan = false;
        this.hascate = false;
    }

    //得到年 月 季度下拉框集合
    @action
    getDateList = () => {
        let now = new Date(); //当前日期
        let cyear = now.getFullYear() + ""; //当前年
        this.yeardata = [];
        for (let i = parseInt(cyear) + 1; i > 2012; i--) {
            if (i == cyear) {
                this.yeardata.push({key: i + "", showname: i, selected: true});
            } else {
                this.yeardata.push({key: i + "", showname: i, selected: false});
            }
        }
    }
    @action
    getAccessFbInfo = (params) => {//反馈信息操作
        params.scoreid = this.scoreid;
        params.viewdate = this.viewdate;
        accessViewApi.accessViewFb(params).then(action(data => {
            if (data.status) {
                if (params.operation == "save") {
                    this.fbcontent = "";
                    this.fbInfoList.unshift(data.smap);
                } else {
                    this.fbInfoList = data.list;
                }
            }
        }))
    }
    @action
    getAccessLogInfo = (params) => {//操作日志
        params.scoreid = this.scoreid;
        params.currentpage = this.currentpage;
        params.total = this.currentpage == 1 ? 0 : this.total;
        accessViewApi.accessViewLog(params).then(action(data => {
            if (data.status) {
                this.currentpage = this.currentpage + 1;
                this.total = data.total;
                if (params.doback) {
                    params.doback();
                }
                if (params.currentpage == 1) {
                    this.logList = data.datas;
                } else {
                    this.logList = this.logList.concat(data.datas);
                }
            }
        }))
    }

    //定性指标计算得分
    @action
    updateScore(index, idx) {
        let record = this.datalist[index];
        let d_score = record["score_" + idx];//得分
        let d_rate2 = 1; //系数  
        if (record.formula == 4) {//针对技术任务量增加相应计算*0.8
            let target = record.itemtarget; //目标值
            let result = record.itemresult; //完成值
            if (result < target) {
                d_rate2 = 0.8;
            }
        }
        record["result_" + idx] = (this.getFloatVal(d_score) * record.rate / 100 * d_rate2).toFixed(2);
        record["real_" + idx] = this.getFloatVal(d_score) * record.rate / 100 * d_rate2;
        this.datalist[index] = record;
        this.countscore(idx);
    }

    //通过定量指标计算得分
    @action
    updateNormScore(index, val) {
        let record = this.datalist[index];
        let target = record.itemtarget; //目标值
        let result = record.itemresult; //完成值
        let d_score = 0;//得分
        let formula = record.formula;
        let d_rate2 = 1; //系数  
        if (record.formula == 4 && result < target) {//针对技术任务量增加相应计算*0.8
            d_rate2 = 0.8;
        }
        if (formula == 13) {//自定义java类
            let params = {};
            params.type1 = this.type1;
            params.target = target;
            params.result = result;
            params.jcway = record.formuladetail;
            params.operation = "formal";
            params.year = this.nowyear;
            params.type2 = this.type2;
            params.resourceid = this.resourceid;
            params.accessitemid = record.itemid;
            if (val && val == 1) {
                params.isauto = val;
            }
            this.loading = true;
            accessViewApi.accessViewOperation(params).then(action(data => {
                if (val && val == 1) {
                    this.canedit = false;
                }
                if (data.result == 0) {
                    d_score = 0;
                } else {
                    d_score = data.resultscore;
                    if (data.item_result) {
                        record["itemresult"] = this.getFloatVal(data.item_result).toFixed(2);
                    }
                }
                if (d_score > this.scoremax) d_score = this.scoremax;
                this.tableHeader && this.tableHeader.length > 0 && this.tableHeader.map((element, idx) => {
                    record["score_" + element.index] = this.getFloatVal(d_score).toFixed(2);
                    record["result_" + element.index] = (this.getFloatVal(d_score) * record.rate / 100 * d_rate2).toFixed(2);
                    record["real_" + element.index] = this.getFloatVal(d_score) * record.rate / 100 * d_rate2;
                    this.datalist[index] = record;
                    this.countscore(element.index);
                });
                if (val && val == 1) {
                    if (this.canedit) {
                        this.canedit = false;//防止多个线程让this.canedit变为true后导致页面不渲染  因此做个判断
                    }
                    this.canedit = true;
                }
                this.loading = false;
            }))
        } else {
            if (formula == 1) {//公式1  完成值/目标值*5  
                if (target != 0) {
                    d_score = result / target * 5;
                }
                if (d_score > 5) d_score = 5;
            } else if (formula == 5) {//公式5 (目标值-完成值)/目标值*5   -----调整为按比例分段控制得分     ------调整为 目标值/完成值*3.5
                if (result != 0 && target != 0) {
                    if (result > target) {
                        let frate = (result - target) / target * 100;//花费比例
                        if (frate <= 10) d_score = 2;
                        else if (frate > 10 && frate <= 20) d_score = 1;
                        else if (frate > 20 && frate <= 30) d_score = 0;
                        else d_score = -1;
                    } else {
                        d_score = target / result * 3.5;
                    }
                } else {
                    d_score = 5;
                    if (target == 0) d_score = 0;
                }
                if (d_score > 5) d_score = 5;
            } else if (formula == 2) {//项目收款
                let result2 = record.validresult;//有效值
                let baseval = 70000;
                if (target < baseval) {
                    d_score = result2 / baseval * 5;
                    if (d_score > 5) d_score = 5;
                } else {
                    if (result > target) {
                        d_score = result2 / baseval * 1 * 5;
                    } else {
                        d_score = result2 / baseval * result / target * 5;
                    }
                }
            } else if (formula == 3) {//项目验收
                let result2 = record.validresult;//有效值
                let baseval = 120000;
                if (target < baseval) {
                    d_score = result2 / baseval * 5;
                    if (d_score > 5) d_score = 5;
                } else {
                    if (result > target) {
                        d_score = result2 / baseval * 1 * 5;
                    } else {
                        d_score = result2 / baseval * result / target * 5;
                    }
                }
            } else if (formula == 11) {//公式11 完成值/目标值*最大分值
                if (target != 0) {
                    d_score = result / target * this.scoremax;
                }
                if (d_score > this.scoremax) d_score = this.scoremax;
            } else if (formula == 12) {//自定义公式
                let formuladetail = record.formuladetail;
                formuladetail = formuladetail.replace(/(gval)/g, target).replace(/(cval)/g, result);
                try {
                    d_score = eval(formuladetail);
                    if (isNaN(result) || 'Infinity'.indexOf(result) > -1) d_score = 0;
                } catch (exception) {
                    d_score = 0;
                }
                if (d_score > this.scoremax) d_score = this.scoremax;
            }
            this.tableHeader && this.tableHeader.length > 0 && this.tableHeader.map(element => {
                record["score_" + element.index] = this.getFloatVal(d_score).toFixed(2);
                record["result_" + element.index] = (this.getFloatVal(d_score) * record.rate / 100 * d_rate2).toFixed(2);
                record["real_" + element.index] = this.getFloatVal(d_score) * record.rate / 100 * d_rate2;
                this.datalist[index] = record;
                this.countscore(element.index, element.userid);
            });
            if (val != 1) {
                this.canedit = true;
            }
        }
    }

    //计算总分
    @action
    countscore(index) {
        let checkscore = 0;//考核得分
        let totalscore = 0;//总得分
        let resultscore = 0;//最终得分

        var userFind = this.tableHeader.find(item => item.index == index);
        //获取当前用户的考核指标项
        var find = this.datalist.find((element, idx) => idx < this.itemcount && element.userid == userFind.userid);
        if (find) {
            this.datalist.map((element, idx) => {
                if (idx < this.itemcount && element.userid == userFind.userid) {
                    checkscore = this.getFloatVal(checkscore) + this.getFloatVal(element["real_" + index]);
                }
            })
        } else {
            this.datalist.map((element, idx) => {
                if (idx < this.itemcount && element.userid == "") {
                    checkscore = this.getFloatVal(checkscore) + this.getFloatVal(element["real_" + index]);
                }
            })
        }

        this.datalist.map((element, idx) => {
            // if (idx < this.itemcount) {
            //     checkscore = this.getFloatVal(checkscore) + this.getFloatVal(element["real_" + index]);
            // }
            if (element.onlyid == 1) { //计算每个评分人的考核得分
                this.datalist[idx]["score_" + index] = checkscore.toFixed(2);
            }
            if (element.onlyid == 2) {//加上修正分
                totalscore = this.getFloatVal(totalscore) + this.getFloatVal(checkscore) + this.getFloatVal(element["score_" + index]);
            }
            if (element.onlyid == 5) {
                this.datalist[idx]["score_" + index] = totalscore.toFixed(2);//总得分
                this.datalist[idx]["scorereal_" + index] = totalscore;//总得分
            }
        });
        this.datalist.map((element, idx) => {
            if (element.onlyid == 5) {
                this.tableHeader.map((d, idx1) => {
                    resultscore = this.getFloatVal(resultscore) + this.getFloatVal(element["scorereal_" + idx1]) * this.getFloatVal(element["prate" + idx1]) / 100;
                });
            }
            if (element.onlyid == 100) {
                for (let i = 0; i < this.tableHeader.length; i++) {
                    this.datalist[idx]["score_" + i] = resultscore.toFixed(2);//总得分
                }
                this.result_score = resultscore.toFixed(2);
            }
        });
    }

    //数据转换
    @action
    getFloatVal(val) {
        if (isNaN(parseFloat(val))) {
            return 0;
        } else {
            return parseFloat(val);
        }
    }

    @action
    saveAccess = (params) => {//保存提交
        params.scoreid = this.scoreid;
        params.resourceid = this.resourceid;
        params.fileids = this.fileids;
        params.datas = JSON.stringify(this.datalist);
        params.result_score = this.result_score;
        params.itemcount = this.itemcount;
        params.delfileids = this.delfileids;
        params.edition = this.edition;
        this.loading = true;
        this.canupload = false;
        accessViewApi.accessViewOperation(params).then(action(data => {
            if (data.status == 1 || data.status == 6) {
                this.accessView({scoreid: this.scoreid});
            } else if (data.status == 5) {
                this.accessView({
                    resourceid: this.resourceid,
                    year: this.nowyear,
                    type1: this.type1,
                    type2: this.type2
                });
            }else {
                this.canupload = true;
            }
            if (params.doback) {
                params.doback(data.status);
            }
            this.loading = false;
        }))
    }
    @action
    doApproveAndReturn = (params) => {//批准和退回
        params.scoreid = this.scoreid;
        this.loading = true;
        accessAuditListApi.accessAuditOperationApi(params).then(action(data => {
            if (data.hasRight) {
                this.accessView({scoreid: this.scoreid, islog: "0"});
            }
            if (params.doback) {
                params.doback(data.api_status, data.hasRight);
            }
            this.loading = false;
        }))
    }
    @action
    doOtherOperation = (params) => {//其他操作
        params.scoreid = this.scoreid;
        this.loading = true;
        accessViewApi.accessViewOperation(params).then(action(data => {
            if (data.status == 1) {
                if (params.operation == "init") {
                    this.accessView({scoreid: data.newscoreid, islog: "0"});
                } else {
                    this.accessView({scoreid: this.scoreid, islog: "0"});
                }
            }
            if (params.doback) {
                params.doback(data.status);
            }
            this.loading = false;
        }))
    }
    @action
    viewDocFiled = (params) => {//附件赋权
        accessViewApi.viewDocFiled(params).then(action(data => {
            if (params.doback) {
                params.doback();
            }
        }))
    }
}